//
//  MTInfoVerificationInputCell.swift
//  MT
//
//  Created by M on 2019/12/22.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

@objc
protocol MTInfoVerificationInputCellDelegate: NSObjectProtocol {
    /// 输入文字
    @objc optional func inputCell(_ cell: BaseMTInfoVerificationCell, infoType: UserProfileInfos, didInputText text: String?)
}

class MTInfoVerificationInputCell: BaseMTInfoVerificationCell {
    
    /// 代理
    weak var delegate: MTInfoVerificationInputCellDelegate?
    
    /// 所有省份
    private var provinces: [ProvinceModel]?
    private var provinceIndex: Int = 0
    private var cityIndex: Int = 0
    private var areaIndex: Int = 0
    
    /// 内容
    var content: String? {
        didSet {
            textField.text = content
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setup()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ Overrides ----------------------
extension MTInfoVerificationInputCell {
    
    override func prepareForReuse() {
        super.prepareForReuse()
        titleLabel.textColor = UIColor(hex: 0x1d1a1a)
        titleLabel.font = PingFangSCSemiboldFont(14)
        underline.backgroundColor = UIColor(hex: 0xe7e7e7)
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension MTInfoVerificationInputCell {
    
    private func setup() {
        textField.tintColor = UIColor(hex: 0x2f2f2f)
        textField.textColor = UIColor(hex:0x1d1a1a)
        textField.font = PingFangSCMediumFont(14)
        textField.addTarget(self, action: #selector(inputCell(textFieldAllEditingEvents:)), for: .allEditingEvents)
    
    }
    
}

// MARK: ------------------ 事件方法 ----------------------
@objc
extension MTInfoVerificationInputCell {
    
    /// 文字输入方法
    private func inputCell(textFieldAllEditingEvents sender: UITextField) {
        if let c = sender.text?.utf8.count, let type = type, type == .nickname, c > type.maximumTextCount ?? 0 {
            titleLabel.textColor = UIColor(hex: 0x1d1a1a)
            underline.backgroundColor = UIColor(hex: 0xe7e7e7)
        } else {
            titleLabel.textColor = UIColor(hex: 0x1d1a1a)
            underline.backgroundColor = UIColor(hex: 0xe7e7e7)
        }
        if let delegate = delegate, let type = type {
            delegate.inputCell?(self, infoType: type, didInputText: sender.text)
        }
    }
    
}
